package com.gabejeff.engine;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;

import javax.swing.Action;

import com.gabejeff.exceptions.InvalidSceneException;

public class GameMenu extends Scene {
	
	private AlphaComposite trans = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.75f);
	private BufferedImage buffer = null;
	private Graphics2D bufg2d = null;
	
	private ConceptualMenu menu;
	
	public GameMenu(boolean fancy) {
		if (fancy) {
			setOverlay(true);
		}
		menu = new ConceptualMenu(new String[]{"Menu", "New Game", "Options", "Help", "Quit"});
		menu.addSubMenu(4, new String[]{"Are you sure?", "Yes", "No"});
		menu.addAction(4, 1, new MenuAction(){

			@Override
			public void performAction() {
				System.exit(0);	
			}
			
		});
		
		menu.addAction(4, 2, new MenuAction(){

			@Override
			public void performAction() {
				menu.goBack();
			}
			
		});
	}

	@Override
	public Scene getSubScene(int x, int y, int width, int height) throws InvalidSceneException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected BufferedImage render() {
		
		dim.setSize(getFrame().getWidth(), getFrame().getHeight());
		
		if (buffer == null || 
			buffer.getWidth() != dim.getWidth() || 
			buffer.getHeight() != dim.getHeight()) {
			
			buffer = new BufferedImage(dim.width, 
									   dim.height, 
									   BufferedImage.TYPE_INT_ARGB);
			
		}

		bufg2d = buffer.createGraphics();
		
		bufg2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

		if (this.isOverlay() && underlying != null) {
			bufg2d.drawImage(underlying, 0, 0, null);
		}
		
		Composite old = bufg2d.getComposite();
		
		if (this.isOverlay()) {
			bufg2d.setComposite(trans);
		}
		
		bufg2d.setColor(Color.BLACK);
		bufg2d.fillRect(0, 0, (int)dim.getWidth(), (int)dim.getHeight());
		
		if (this.isOverlay()) {
			bufg2d.setComposite(old);
		}
		
		menu.draw(bufg2d, dim);
		
		bufg2d.dispose();
				
		return buffer;
	}

	@Override
	public void moveUp() {
		menu.moveUp();
	}

	@Override
	public void moveDown() {
		menu.moveDown();
	}

	@Override
	public void moveLeft() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void moveRight() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void performMenuAction(boolean escape) {
		
		if (escape) {
			if (menu.isTopLevel()) {
				menu.reset();
				forceDone();
			} else {
				menu.goBack();
				return;
			}
		}
		
		menu.performAction();
		
	}
	
}
